﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.ComponentModel;
using System.Web;

namespace WOSDesktop.Plugins.Facebook
{
    /// <summary>
    /// Interaction logic for VideoDownload.xaml
    /// </summary>
    public partial class VideoDownload : Window
    {
        public VideoDownload()
        {
            InitializeComponent();
        }
        public void Download()
        {
            List<DTask> tasks = new List<DTask>();
            DTask t = new DTask();
            t.TaskID = Guid.NewGuid();
            t.Url = this.txtUrl.Text;
            t.Path = this.txtPath.Text;
            t.ProgressChanged += new EventHandler<TaskProgressChangedEventArgs>(t_ProgressChanged);
            t.Completed += new EventHandler<TaskCompletedEventArgs>(t_Completed);
            t.FileAsync();


        }

        void t_Completed(object sender, TaskCompletedEventArgs e)
        {
            MessageBox.Show("Complated");
        }

        void t_ProgressChanged(object sender, TaskProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }
        public class TaskManager
        {

        }
        
        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            Download();
        }
    }
    public class WebTask
    {
        protected WebClient webClient;
        public WebTask()
        {
            webClient = new WebClient();

        }
        public Guid TaskID { get; set; }
        public string Url { get; set; }
        public string Path { get; set; }
        public string FileName(string url)
        {
            Uri uri = new Uri(url);
            return System.IO.Path.GetFileName(uri.LocalPath);
        }
        public int ProgressPercentage { get; set; }
        public bool Completed { get; set; }
    }
    public class TaskProgressChangedEventArgs : EventArgs
    {
        public TaskProgressChangedEventArgs(WebTask task, int progressPercentage, object userState)
        {
            this.Task = task;
            this.ProgressPercentage = progressPercentage;
            this.UserState = userState;
        }
        public WebTask Task { get; set; }
        public int ProgressPercentage { get; private set; }
        public object UserState { get; private set; }
    }
    public class DTaskProgressChangedEventArgs : TaskProgressChangedEventArgs
    {

        public DTaskProgressChangedEventArgs(WebTask task, int progressPercentage, object userState,
           long bytesReceived, long totalBytesToReceive)
            : base(task, progressPercentage, userState)
        {
            this.BytesReceived = bytesReceived;
            this.TotalBytesToReceive = totalBytesToReceive;
        }
        public long BytesReceived { get; private set; }
        public long TotalBytesToReceive { get; private set; }
    }
    public class UTaskProgressChangedEventArgs : DTaskProgressChangedEventArgs
    {
        public UTaskProgressChangedEventArgs(WebTask task, int progressPercentage, object userState,
           long bytesReceived, long totalBytesToReceive, long bytesSent, long totalBytesToSend)
            : base(task, progressPercentage, userState, bytesReceived, totalBytesToReceive)
        {
            this.BytesSent = bytesSent;
            this.TotalBytesToSend = totalBytesToSend;
        }
        public long BytesSent { get; private set; }
        public long TotalBytesToSend { get; private set; }
    }


    public class TaskCompletedEventArgs : EventArgs
    {
        public TaskCompletedEventArgs(WebTask task, Exception error, bool cancelled, object userState)
        {
            this.Task = task;
            this.Cancelled = cancelled;
            this.UserState = userState;
            this.Error = error;
        }
        public bool Cancelled { get; private set; }
        public object UserState { get; private set; }
        public Exception Error { get; private set; }
        public WebTask Task { get; set; }
    }


    public class DTask : WebTask
    {
        public void FileAsync()
        {
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(OnCompleted);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnProgressChanged);
            webClient.DownloadFileAsync(new Uri(this.Url), this.Path + @"\" + FileName(Url));
        }
        public void OnProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            ProgressChanged(this, new TaskProgressChangedEventArgs(this, e.ProgressPercentage, e.UserState));
        }
        private void OnCompleted(object sender, AsyncCompletedEventArgs e)
        {
             if (Completed != null)
                 Completed(this, new TaskCompletedEventArgs(this,e.Error,e.Cancelled,e.UserState));
        }
        public event EventHandler<TaskProgressChangedEventArgs> ProgressChanged;
        public event EventHandler<TaskCompletedEventArgs> Completed;
    }
    public class UTask : WebTask
    {
        public void FileAsync()
        {
            webClient.UploadFileCompleted += new UploadFileCompletedEventHandler(webClient_UploadFileCompleted);
            webClient.UploadProgressChanged += new UploadProgressChangedEventHandler(webClient_UploadProgressChanged);
            webClient.UploadFileAsync(new Uri(this.Url), this.Path);
        }
        void webClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            //if (ProgressChanged != null)
            //    ProgressChanged(this, e);
        }
        void webClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            //if (Completed != null)
            //    Completed(this, e);
        }
        
    }
}
